const path = require("path");

const runShell = require('./runShell');



// REPOS——path
const reposPath = path.resolve(__dirname, "./../.templateRepo");
// gp27-template-list
const targetPath = `${reposPath}/gp27-template-list`;



/**
 * 
 * @param {*}
 *      - url string  仓库地址url
 *      - name string 可选 clone到本地后的名字
 * @returns Promise<ShellString>
 */
const clone = (url, name) => {
    return runShell(`git clone ${url} ${name ? name : ''}`, reposPath);
}

const pull = () => {
    return runShell(`git pull`, targetPath);   
}

const checkout = (branch) => {
    return runShell(`git checkout ${branch}`, targetPath)
}

const pushHead  = (args) => {
    return runShell (`git push origin HEAD ${args}`, targetPath)
}

const getBranchs = () => runShell('git branch -a', targetPath).then((res = '')=>{
    const list =  res.split('\n')
            .map(item => item.replace(/(remotes\/origin\/)|(\*)/g, '').trim())
            .filter(item => item && !item.includes('->'))
    return Array.from(new Set(list));
});

module.exports = {
    clone,
    pull,
    checkout,
    pushHead,
    getBranchs
}